<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <title>设置</title>
    <link rel="stylesheet" type="text/css" href="../css/aui.css"/>
    <link rel="stylesheet" type="text/css" href="../css/my.css"/>
    <link rel="stylesheet" type="text/css" href="../css/common.css"/>
    <style>
        .aui-list{background: transparent;border:none;}
        .aui-list .aui-list-item{background: #fff;border-radius: .3rem;padding-top: 3px;padding-bottom: 3px;border:none;background-image: none!important;}
        .aui-list .aui-list-header{background:transparent;color: #808080;justify-content:left;}

    </style>
</head>
<body>
<div class="aui-content-padded aui-margin-b-15">
    <ul class="aui-list aui-list-in">
        <li class="aui-list-item aui-list-item-middle" onclick="bind_mobile()" tapmode >
            <div class="aui-list-item-inner aui-list-item-arrow">
                <div class="aui-list-item-title">绑定手机</div>
                <div id="mobile">去绑定</div>
            </div>
        </li>
        <li class="aui-list-header"></li>
        <li class="aui-list-item aui-list-item-middle" onclick="bind_wechat()" tapmode >
            <div class="aui-list-item-inner aui-list-item-arrow">
                <div class="aui-list-item-title">绑定微信</div>
                <div id="wechat">去绑定</div>
            </div>
        </li>
        <li class="aui-list-header"></li>
        <li class="aui-list-item aui-list-item-middle" onclick="clear_cache()">
            <div class="aui-list-item-inner aui-list-item-arrow">
                <div class="aui-list-item-title">清除缓存</div>
                <div id="cachesize"></div>
            </div>
        </li>
        <li class="aui-list-header"></li>
        <li class="aui-list-item">
            <div class="aui-list-item-inner">
                <div class="aui-list-item-title">应用版本</div>
                <div id="version"></div>
            </div>
        </li>

    </ul>
</div>
<div class="aui-text-center"><p>Copyright © 2018 惠好物 版权所有 </p></div>
<div class="aui-content-padded aui-margin-t-15">
   <div class="aui-btn aui-btn-block tuichu jb" onclick="logoutd()" tapmode>退出登录</div>
</div>
</body>
<script type="text/javascript" src="../script/api.js" ></script>
<script type="text/javascript" src="../script/zepto.min.js" ></script>
<script type="text/javascript" src="../script/common.js" ></script>
<script type="text/javascript">
    var uinfo;
    apiready = function(){
        api.parseTapmode();
        //异步返回结果：
        api.getCacheSize(function(ret) {
            var size = ret.size;
            $('#cachesize').text(parseFloat(size/1024/1024).toFixed(2)+'M');
        });
        var appVersion = api.appVersion;
        $('#version').text(appVersion);
        //绑定检测
        uinfo = JSON.parse(_GetPrefs('uinfo'));
        if (uinfo.is_bind_wx!=0) {
            $('#wechat').text('已绑定');
        }
        if (uinfo.is_bind_mobile!=0) {
            $('#mobile').text(uinfo.is_bind_mobile)
        }
    };
    //清除缓存
    function clear_cache(){
        api.confirm({
            title: '清除缓存数据',
            msg: '将会清除所有的缓存数据',
            buttons: ['确定', '取消']
        }, function(ret, err) {
            var index = ret.buttonIndex;
            if (index!=1) return false;
            api.clearCache(function() {
                _toast('清除完成');
                 _RefreshPage();
            });
        });

    }
    //退出
    function logoutd(){
        _RemovePrefs('uid');
        _RemovePrefs('token');
        _RemovePrefs('uinfo');
        //广播事件：退出登录
        _SendEvent('UserLogout');
        closeWin();
    }
    //绑定手机
    function bind_mobile(){
        if (uinfo.is_bind_mobile!=0) {
            _toast('您已绑定！');
        }else{
            openPublic(JSON.stringify({title:'绑定手机',framename:'bind_mobile_frm'}));
        }
    }
    //绑定微信
    function bind_wechat(){
        if (uinfo.is_bind_wx!=0) {
            _toast('您已绑定！');
            return false;
        }
        var wx = api.require('wx');
        //是否安装客户端
        wx.isInstalled(function(ret, err) {
            if (!ret.installed) {
                alert('您还没安装微信客户端');
                return false;
            }else{
                //step1 获取code...
                wx.auth({
                    apiKey: ''
                }, function(ret, err) {
                    //step get access_token
                    if (ret.status) {
                        _loading('登录中...','',true);
                        wx.getToken({
                            apiKey: '',
                            apiSecret: '',
                            code: ret.code
                        }, function(ret, err) {
                            if (ret.status) {
                                //step3 getuserinfo
                                wx.getUserInfo({
                                    accessToken: ret.accessToken,
                                    openId: ret.openId
                                }, function(ret, err) {
                                    if (ret.status) {
                                        //last 提交服务器验证
                                        var uinfo = ret;
                                        _Ajax(window.APIServer.BindWechat,'post',{values:uinfo},'json',function(ret){
                                            _loading_close();
                                            if (ret.code==1) {
                                                //广播事件：用户绑定微信成功
                                                _SendEvent('bind_mobile_wechat');
                                                _toast('绑定成功！');
                                                setTimeout(function() {
                                                     api.closeToWin({
                                                         name : 'root'
                                                     });
                                                }, 500)
                                            }else{
                                                _toast(ret.msg);
                                            }

                                        });
                                    } else {
                                        _loading_close();
                                        _toast('获取信息失败');
                                    }
                                });
                            } else {
                                _loading_close();
                                _toast('绑定失败');
                            }
                        });
                    } else {
                        var code_arr = {'-1':'未知错误','1':'您取消了登录','2':'您拒绝了登录','3':'微信未安装'};
                        _toast(code_arr[err.code]);
                    }
                });
            }
        });

    }
</script>
</html>
